import { Random } from 'mockjs'
import { getGetRequestParameters } from '../../utils/getRequestParameterss'

//请求
//对于中国指数的模拟请求
export function getChinaIndex(req) {
  const params = getGetRequestParameters(req.url)

  const indices = []
  for (let i = 0; i < params.page_size; i++) {
    indices.push({
      index_name: Random.cname(),
      lastest_price: Random.natural(1, 10000),
    })
  }
  return {
    code: 200,
    data: {
      status: 'success',
      data: {
        current_page: params.page,
        total_pages: 10,
        indices: indices,
      },
    },
  }
}

//对于新股上市的模拟请求
export function getNewListing(req) {
  const params = getGetRequestParameters(req.url)

  const stocks = []
  for (let i = 0; i < params.page_size; i++) {
    stocks.push({
      stock_name: Random.csentence(6),
      listing_data: Random.date('yyyy-MM-dd'),
    })
  }
  return {
    code: 200,
    data: {
      status: 'success',
      data: {
        current_page: params.page,
        total_pages: 10,
        stocks: stocks,
      },
    },
  }
}

//对于证券快讯的模拟请求
export function getSecuritiesExpress(req) {
  const params = getGetRequestParameters(req.url)

  const info = []
  for (let i = 0; i < Random.integer(1, 20); i++) {
    info.push({
      type: getRandomString1(),
      title: Random.cparagraph(),
      time: Random.date('yyyy-MM-dd') + 'T' + Random.time('HH:mm:ss') + 'Z',
    })
  }
  return {
    code: 200,
    data: {
      status: 'success',
      data: info,
    },
  }
}

//对于港股通资金流的模拟请求
export function getHongKongStockConnectFundFlow(req) {
  const params = getGetRequestParameters(req.url)

  const info = []
  for (let i = 0; i < 4; i++) {
    let balance = Random.integer(100, 1000)
    info.push({
      sector_name: Random.csentence(4),
      balance: balance,
      quota: balance + Random.integer(100, 1000),
      net_inflow: Random.integer(-100, 100),
    })
  }
  return {
    code: 200,
    data: {
      status: 'success',
      data: info,
    },
  }
}

//对于板块资金流的模拟请求
export function getSectorFlows(req) {
  const params = getGetRequestParameters(req.url)

  const gainers = []
  const losers = []
  for (let i = 0; i < 4; i++) {
    gainers.push({
      sector_name: Random.csentence(4),
      change: Random.float(0, 100, 2, 2),
    })
    losers.push({
      sector_name: Random.csentence(4),
      change: Random.float(-100, 0, 2, 2),
    })
  }
  return {
    code: 200,
    data: {
      status: 'success',
      data: {
        top_gainers: gainers,
        top_losers: losers,
      },
    },
  }
}

//随机获取“info”或“alert”
function getRandomString1() {
  const strings = ['info', 'alert']
  const randomIndex = Math.floor(Math.random() * strings.length)
  return strings[randomIndex]
}
